(function ($) {
    'use strict';
    let controller = {
        init: function () {
            let cell = this.arrToCell(this.getMain());
            let result = this.getSort(cell);
            $('#box').html(template('tpl', {data:result}));
            console.log(result)
        },
        getMain: function () {
            let main = [];
            for (let i = 1; i < 10; i++) {
                main.push(i);
            }

            main.sort(function () {
                return 0.5 - Math.random();
            });
            return main;
        },
        arrToCell: function (arr) {
            let cell = [];
            $.each(arr, function (index, el) {
                let i = Math.floor(index / 3);
                if (!cell[i]) {
                    cell[i] = [];
                }
                cell[i].push(el);
            });
            return cell;
        },
        getSib: function (arr) {
            let ab1 = [1, 2, 0];
            let ab2 = [2, 0, 1];
            let arr1 = [arr[ab1[0]], arr[ab1[1]], arr[ab1[2]]];
            let arr3 = [arr[ab2[0]], arr[ab2[1]], arr[ab2[2]]];
            return [arr1, arr, arr3];
        },
        rep: function (arr) {
            let tmp = [];
            $.each(arr, function (index, el) {
                if (!tmp[0]) {
                    tmp[0] = [];
                }
                if (!tmp[1]) {
                    tmp[1] = [];
                }
                if (!tmp[2]) {
                    tmp[2] = [];
                }
                tmp[0].push(el[0]);
                tmp[1].push(el[1]);
                tmp[2].push(el[2]);
            });
            return tmp;
        },
        getSort: function (cell) {
            // var off1 = ['x','y'];
            // var off2 = [0,1];
            // off1.sort(function(){ return 0.5 - Math.random() });
            // off2.sort(function(){ return 0.5 - Math.random() });
            // var kas = [[1,2,0],[2,0,1]];
            // var ba1 = kas[off2[0]];
            // var ba2 = kas[off2[1]];
            let lr = this.getSib(cell);
            let re4 = lr[0];
            let re5 = lr[1];
            let re6 = lr[2];
            let ltp = this.getSib(this.rep(re4));
            let re1 = this.rep(ltp[0]);
            let re7 = this.rep(ltp[2]);
            let rtp = this.getSib(this.rep(re6));
            let re3 = this.rep(rtp[0]);
            let re9 = this.rep(rtp[2]);
            let ctp = this.getSib(this.rep(re5));
            let re2 = this.rep(ctp[0]);
            let re8 = this.rep(ctp[2]);
            return [re1, re2, re3, re4, re5, re6, re7, re8, re9];
        }
    };
    controller.init();
}(jQuery));